"use strict";
cc._RF.push(module, 'dad82TfvodB7ZTO0/fteMPV', 'CsvDataManage');
// scripts/engines/managers/CsvDataManage.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var gameEngine_1 = require("../gameEngine");
var CsvFileKind_1 = require("../libs/CsvFileKind");
var data_control_1 = require("../libs/data_control");
var utilsModel_1 = require("../libs/utilsModel");
var superManager_1 = require("./superManager");
var CsvDataManage = /** @class */ (function (_super) {
    __extends(CsvDataManage, _super);
    function CsvDataManage() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.gameCsvData = {};
        return _this;
    }
    CsvDataManage.prototype.loadManager = function () {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, Promise.resolve()];
            });
        });
    };
    CsvDataManage.prototype.initCsvList = function (loadBack, loadFinish) {
        return __awaiter(this, void 0, void 0, function () {
            var _this = this;
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (resolve) {
                        var CsvFile = CsvFileKind_1.CsvFileKind.CsvFile;
                        var csvFile = [
                            CsvFile.peopleArcheryTimeLine,
                            CsvFile.peopleArcheryTimeLineFile0,
                            CsvFile.peopleArcheryTimeLineFile1,
                            CsvFile.peopleArcheryTimeLineFile2,
                            CsvFile.peopleArcheryTimeLineFile3,
                            CsvFile.peopleArcheryOption,
                            CsvFile.peopleArcheryOptionDoor,
                            CsvFile.peopleArcheryBox,
                            CsvFile.cardCompositionLevel,
                            CsvFile.bunchbunchbunchLevel,
                            CsvFile.goldMinerLevel,
                            CsvFile.goldMinerHookLevel,
                            CsvFile.goldMinerHoookGooods,
                            CsvFile.goldMinerChats,
                            CsvFile.peopleArcheryTimeLineFile4,
                            CsvFile.peopleArcheryInfinityEnemyHp,
                            CsvFile.goldMinerProp,
                            CsvFile.peopleArcheryRole,
                            CsvFile.peopleArcheryTalent,
                            CsvFile.peopleArcheryMagicWeapon,
                            CsvFile.peopleArcherySurviveCatastrophe,
                        ];
                        var csvMapFile = [];
                        var csvFileIndx = 0;
                        var csvFileLength = csvFile.length;
                        for (var _i = 0, csvFile_1 = csvFile; _i < csvFile_1.length; _i++) {
                            var file = csvFile_1[_i];
                            _this.loadCsvFile(file, false, function () {
                                csvFileIndx++;
                                if (loadBack) {
                                    loadBack(csvFileIndx / csvFileLength);
                                }
                                if (csvFileLength <= csvFileIndx) {
                                    // 加载完成
                                    if (loadFinish) {
                                        loadFinish();
                                    }
                                    resolve();
                                }
                            });
                        }
                        for (var _a = 0, csvMapFile_1 = csvMapFile; _a < csvMapFile_1.length; _a++) {
                            var file = csvMapFile_1[_a];
                            _this.loadCsvFile(file, true);
                        }
                    })];
            });
        });
    };
    CsvDataManage.prototype.loadCsvFile = function (filePath, bFirstMap, onComplete) {
        var _this = this;
        if (bFirstMap === void 0) { bFirstMap = false; }
        var bundle = gameEngine_1.default.getModules().uiControlModule.getGameBundle();
        utilsModel_1.default.readCsvData(bundle, filePath, function (_data) {
            var _a;
            var data = {};
            data['title'] = {};
            for (var i in _data[1]) {
                data['title'][_data[1][i]] = i;
            }
            var idIndex = (_a = data['title'].id) !== null && _a !== void 0 ? _a : 0;
            for (var i = 2; i < _data.length; i++) {
                if (bFirstMap) {
                    if (!data[_data[i][idIndex]]) {
                        data[_data[i][idIndex]] = [];
                    }
                    data[_data[i][idIndex]].push(_data[i]);
                }
                else {
                    data[_data[i][idIndex]] = _data[i];
                }
            }
            _this.gameCsvData[filePath] = data;
            // Logger.logModel(`CSV File ${name} load Finish`);
            if (onComplete) {
                onComplete();
            }
        });
    };
    CsvDataManage.prototype.getCsvData = function (name) {
        if (this.gameCsvData == null)
            return null;
        if (this.gameCsvData[name] == null) {
            console.warn("\u6B64\u6587\u4EF6\u672A\u52A0\u8F7D " + name);
            return null;
        }
        return this.gameCsvData[name];
    };
    CsvDataManage.prototype.resetCsvData = function (name) {
        this.gameCsvData[name] = null;
    };
    /**
     * 获取csv中的数据
     * @param csvName 文件名
     * @param id id
     * @param itemName 字段名字，null则是全部字段
     * @returns
     */
    CsvDataManage.prototype.getCsvDataItem = function (csvName, id, itemName) {
        var _infos = this.getCsvData(csvName);
        if (!_infos) {
            return null;
        }
        var result = {};
        var _title = _infos.title;
        var info = _infos[id];
        if (!info) {
            return null;
        }
        if (!itemName) {
            for (var n in _title) {
                result[n] = info[_title[n]];
            }
        }
        else {
            for (var _i = 0, itemName_1 = itemName; _i < itemName_1.length; _i++) {
                var o = itemName_1[_i];
                result[o] = info[_title[o]];
            }
        }
        return result;
    };
    CsvDataManage.prototype.getCsvRandomId = function (csvName) {
        var csvInfos = this.getCsvData(csvName);
        if (!csvInfos) {
            console.error("\u6570\u636E\u672A\u627E\u5230");
            return null;
        }
        var idArr = this.getCsvIds(csvName);
        var randomIdx = data_control_1.default.randomNumInt(0, idArr.length - 1);
        var id = idArr[randomIdx];
        return id;
    };
    CsvDataManage.prototype.getCsvRandomIdArr = function (csvName, randomCount) {
        var csvInfos = this.getCsvData(csvName);
        if (!csvInfos) {
            console.error("\u6570\u636E\u672A\u627E\u5230");
            return null;
        }
        var idArr = this.getCsvIds(csvName);
        var randomIdx = data_control_1.default.randomIntNum(0, idArr.length - 1, randomCount);
        var retArr = [];
        for (var _i = 0, randomIdx_1 = randomIdx; _i < randomIdx_1.length; _i++) {
            var o = randomIdx_1[_i];
            retArr.push(idArr[o]);
        }
        return retArr;
    };
    CsvDataManage.prototype.getCsvIds = function (csvName) {
        var csvInfos = this.getCsvData(csvName);
        if (!csvInfos) {
            console.error("\u6570\u636E\u672A\u627E\u5230");
            return [];
        }
        var idArr = [];
        for (var i in csvInfos) {
            if (i == 'title' || i == '' || i.trim() == '') {
                continue;
            }
            idArr.push(i);
        }
        return idArr;
    };
    return CsvDataManage;
}(superManager_1.default));
exports.default = CsvDataManage;

cc._RF.pop();